﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Repositories.Account;

namespace Gfw.Infrastructure.Facilities
{
    /// <summary>
    /// 公司帐户结算，用于定时计算公司帐户信息
    /// </summary>
    public class AccountSettlement : IFacility
    {
        private DateTime _end;

        public AccountSettlement(DateTime end)
        {
            _end = end;
        }


        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            if (unitOfWork != null)
                throw new GfwTransactionInvalidException("公司帐户计算，必须以独立事务运行。");

            var accountRep = new ActCompanyAccountinformationRepository();
            var account = accountRep.Unique(unitOfWork);

            var rep = new ActCompanyBargainRepository();
            var profit = rep.GetProfit(account.SettlementDate.Value, _end);
            var bonus = rep.GetBonus(account.SettlementDate.Value, _end);

            account.Profit += profit;
            account.Bonus += bonus;

            account.SettlementDate = _end;
            accountRep.Update(account);

        }
    }
}
